﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dimebrain.TweetSharp.Extensions;
using Dimebrain.TweetSharp.Fluent;
using Dimebrain.TweetSharp.Model;
using System.Text.RegularExpressions;

namespace TwitterTestApp
{
    [Serializable]
    public partial class DirectMessage : IComparable
    {
        [Flags]
        public enum Types
        {
            All = 0, Task = 1, Urgent = 2, Unread = 4,
            //All = Urgent | Task | Unread
        }

        public TwitterDirectMessage TDM { get; set; }

        public Types MessageTypes { get; set; }

        public bool IsTask
        {
            get { return (MessageTypes & Types.Task) != 0; }
            set
            {
                // Set flag if this message is a task
                if (value == true)
                {
                    MessageTypes |= Types.Task;
                    // Change the color only if it's not urgent
                    if (!IsUrgent) { }
                }

                if (value == false)
                {
                    if (IsTask) MessageTypes ^= Types.Task; // Remove flag
                }

            }
        }

        public bool IsUrgent
        {
            get { return (MessageTypes & Types.Urgent) != 0; }
            set
            {
                if (value == true)
                {
                    MessageTypes |= Types.Urgent;
                }
                else if (value == false && IsUrgent == true)
                {
                    MessageTypes ^= Types.Urgent;
                }
            }
        }

        public bool IsNew
        {
            get { return (MessageTypes & Types.Unread) != 0; }
            set
            {


                if (value == true)
                {
                    MessageTypes |= Types.Unread;
                    if (!IsTask && !IsUrgent) { }
                }
                else if (IsNew) MessageTypes ^= Types.Unread;
            }
        }

        public DirectMessage(TwitterDirectMessage dm)
        {
            this.TDM = dm;
            this.parseMessage(dm.Text);
        }

        public Message createMessageBox()
        {
            Message mb = new Message(this);
            return mb;
        }

        private void parseMessage(string message)
        {
            String match = @"\s*(?<imp>!+)?\s*(?<task>task)?\s*(?<imp>!+)?\s*(?<message>.*)";
            Regex r = new Regex(match);
            Match m = r.Match(message);
            if (m.Groups["task"].Success)
            {
                this.IsTask = true;
            }
            if (m.Groups["imp"].Success)
            {
                this.IsUrgent = true;
            }
            if (m.Groups["message"].Success)
            {
                this.TDM.Text = m.Groups["message"].Value;
            }

        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            if (obj is DirectMessage)
            {
                DirectMessage dm2 = (DirectMessage)obj;
                return this.CompareTo(dm2);
            }
            else
                throw new ArgumentException("Object is not a DirectMessage.");
        }

        #endregion

        #region IComparable Members

        int IComparable.CompareTo(object obj)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
